#include "PlayerStateAttackAir.h"
#include "Player.h"
#include "EntityManager.h"
#include "ZeroAttack.h"

PlayerStateAttackAir::PlayerStateAttackAir(Player* player, sf::Sound* attackSFX, EntityManager* em) : 
PlayerState(player),
_attackSFX(attackSFX),
_entityManager(em)
{}

PlayerStateAttackAir::~PlayerStateAttackAir(void){}

void PlayerStateAttackAir::activate()
{
	_player->setFlipChangeAllow(false);
	_player->getAnimationMgr().setAnimation("cut_air");
	_attackSFX->play();
	_za = new ZeroAttack(_player, sf::FloatRect(0.0f, 0.0f, 38.0f, 32.0f), sf::Vector2f(10.0f * _player->getFlip(), 0.0f), _player->getNormalDamage());
	_entityManager->addEntity(_za);
}

void PlayerStateAttackAir::deactivate()
{
	_player->setFlipChangeAllow(true);
	_za->kill();
}

int PlayerStateAttackAir::update()
{
	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;

	if (_player->_isJumping)
	{
		_player->_velocity.y = -(_player->getJumpPower() * GameTime::getSingleton()->deltaTime());

		if (_player->_position.y < _player->_jumpStartPosition + _player->getJumpLength())
		{
			_player->setFlag(GRAVITY_AFFECTED);
			_player->_isJumping = false;
		}
		else if (_player->getControlState()->isKeyReleased(CControls::Key(CTRL_JUMP, 0), 0))
		{
			_player->setFlag(GRAVITY_AFFECTED);
			_player->_isJumping = false;
		}
	}
	
	_player->applyVelocity();
	
	if ((_player->flags() & JUST_LANDED) == JUST_LANDED)
	{
		_player->removeFlag(JUST_LANDED);
		_player->resetDash();
		return PSRV_LAND;
	}
	else if (_player->getAnimationMgr().getCurrentID() == "cut_air")
	{
		if (_player->getAnimationMgr().isDone() && _player->_isJumping)
			return PSRV_JUMPING;
		else if (_player->getAnimationMgr().isDone() && !_player->_isJumping)
			return PSRV_FALLING;

	}
	else if (_player->_onGround)
	{
		_player->resetDash();
		return PSRV_LAND;
	}

	return PSRV_NOTHING;
}